//
//  HLTabBar.swift
//  HLWeiBo
//
//  Created by jack on 15/12/5.
//  Copyright © 2015年 jack. All rights reserved.
//

import UIKit

class HLTabBar: UITabBar {
    
    //定义闭包的变量
    var composeBtnClorsure: (()->())?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpUI()
    }
    

    func setUpUI() {
        backgroundImage = UIImage(named: "tabbar_background")
        addSubview(addButton)
    }
    // MARK: - 给tabbar布局
    override func layoutSubviews() {
        
        super.layoutSubviews()

        addButton.center = CGPoint(x: frame.width * 0.5, y: frame.height * 0.5)
        
        let btnWidth = frame.width / 5
        
        var index = 0
        
        for childView in subviews {

            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                let x = CGFloat(index) * btnWidth
                
                childView.frame = CGRect(x: x, y: 0, width: btnWidth, height: frame.height)
                
                index++
                
                if index == 2 {
                
                    index++
                }
            }
        }
    }
    
    @objc private func composeBtnClick() {

        print("按钮点击")
        
        composeBtnClorsure?()
    }
    
    // MARK: - 懒加载加号btn
    lazy var addButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "composeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
    
        return button
    }()
    

    
}
